home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevtfax.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.4 KB  |  362 lines

  1. /* Copyright (C) 1994, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevtfax.c,v 1.4 2000/09/19 19:00:23 lpd Exp $ */
  20. /* TIFF and TIFF/fax devices */
  21. #include "gdevprn.h"
  22. #include "gdevtifs.h"
  23. #include "strimpl.h"
  24. #include "scfx.h"
  25. #include "gdevfax.h"
  26. #include "gdevtfax.h"        /* for gdev_fax_print_page_stripped proto */
  27.  
  28. /* ---------------- TIFF/fax output ---------------- */
  29.  
  30. /* The device descriptors */
  31.  
  32. private dev_proc_get_params(tfax_get_params);
  33. private dev_proc_put_params(tfax_put_params);
  34. private dev_proc_print_page(tiffcrle_print_page);
  35. private dev_proc_print_page(tiffg3_print_page);
  36. private dev_proc_print_page(tiffg32d_print_page);
  37. private dev_proc_print_page(tiffg4_print_page);
  38.  
  39. struct gx_device_tfax_s {
  40.     gx_device_common;
  41.     gx_prn_device_common;
  42.     gx_fax_device_common;
  43.     long MaxStripSize;        /* 0 = no limit, other is UNCOMPRESSED limit */
  44.     gdev_tiff_state tiff;    /* for TIFF output only */
  45. };
  46. typedef struct gx_device_tfax_s gx_device_tfax;
  47.  
  48. /* Define procedures that adjust the paper size. */
  49. private const gx_device_procs gdev_tfax_std_procs =
  50.     prn_params_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  51.              tfax_get_params, tfax_put_params);
  52.  
  53. #define TFAX_DEVICE(dname, print_page)\
  54. {\
  55.     FAX_DEVICE_BODY(gx_device_tfax, gdev_tfax_std_procs, dname, print_page),\
  56.     0                /* unlimited strip size byte count */\
  57. }
  58.  
  59. const gx_device_tfax gs_tiffcrle_device =
  60.     TFAX_DEVICE("tiffcrle", tiffcrle_print_page);
  61.  
  62. const gx_device_tfax gs_tiffg3_device =
  63.     TFAX_DEVICE("tiffg3", tiffg3_print_page);
  64.  
  65. const gx_device_tfax gs_tiffg32d_device =
  66.     TFAX_DEVICE("tiffg32d", tiffg32d_print_page);
  67.  
  68. const gx_device_tfax gs_tiffg4_device =
  69.     TFAX_DEVICE("tiffg4", tiffg4_print_page);
  70.  
  71. /* Get/put the MaxStripSize parameter. */
  72. private int
  73. tfax_get_params(gx_device * dev, gs_param_list * plist)
  74. {
  75.     gx_device_tfax *const tfdev = (gx_device_tfax *)dev;
  76.     int code = gdev_fax_get_params(dev, plist);
  77.     int ecode = code;
  78.  
  79.     if ((code = param_write_long(plist, "MaxStripSize", &tfdev->MaxStripSize)) < 0)
  80.         ecode = code;
  81.     return ecode;
  82. }
  83. private int
  84. tfax_put_params(gx_device * dev, gs_param_list * plist)
  85. {
  86.     gx_device_tfax *const tfdev = (gx_device_tfax *)dev;
  87.     int ecode = 0;
  88.     int code;
  89.     long mss = tfdev->MaxStripSize;
  90.     const char *param_name;
  91.  
  92.     switch (code = param_read_long(plist, (param_name = "MaxStripSize"), &mss)) {
  93.         case 0:
  94.         /*
  95.          * Strip must be large enough to accommodate a raster line.
  96.          * If the max strip size is too small, we still write a single
  97.          * line per strip rather than giving an error.
  98.          */
  99.         if (mss >= 0)
  100.             break;
  101.         code = gs_error_rangecheck;
  102.     default:
  103.         ecode = code;
  104.         param_signal_error(plist, param_name, ecode);
  105.     case 1:
  106.         break;
  107.     }
  108.  
  109.     if (ecode < 0)
  110.     return ecode;
  111.     code = gdev_fax_put_params(dev, plist);
  112.     if (code < 0)
  113.     return code;
  114.  
  115.     tfdev->MaxStripSize = mss;
  116.     return code;
  117. }
  118.  
  119. /* Send the page to the printer. */
  120. /* Print a page with a specified width, which may differ from    */
  121. /* the width stored in the device. The TIFF file may have    */
  122. /* multiple strips of height 'rows'.                */
  123. private int
  124. gdev_stream_print_page_strips(gx_device_printer * pdev, FILE * prn_stream,
  125.                   const stream_template * temp, stream_state * ss,
  126.                   int width, long rows_per_strip)
  127. {
  128.     gx_device_tfax *const tfdev = (gx_device_tfax *)pdev;
  129.     int row, row_next;
  130.     int code = 0;
  131.  
  132.     for (row = 0; row < pdev->height; row = row_next) {
  133.     row_next = min(row + rows_per_strip, pdev->height);
  134.     code = gdev_fax_print_strip(pdev, prn_stream, temp, ss, width,
  135.                     row, row_next);
  136.     if (code < 0)
  137.         break;
  138.     gdev_tiff_end_strip(&tfdev->tiff, prn_stream);
  139.  
  140.     }
  141.     return code;
  142. }
  143.  
  144. /* Print a page with a specified width, which may differ from the */
  145. /* width stored in the device. */
  146. private int
  147. gdev_stream_print_page_width(gx_device_printer * pdev, FILE * prn_stream,
  148.                  const stream_template * temp, stream_state * ss,
  149.                  int width)
  150. {
  151.     return gdev_stream_print_page_strips(pdev, prn_stream, temp, ss,
  152.                      width, pdev->height);
  153. }
  154.  
  155. private int
  156. gdev_stream_print_page(gx_device_printer * pdev, FILE * prn_stream,
  157.                const stream_template * temp, stream_state * ss)
  158. {
  159.     return gdev_stream_print_page_width(pdev, prn_stream, temp, ss,
  160.                     pdev->width);
  161. }
  162.  
  163. /* Print a fax page.  Other fax drivers use this. */
  164. int
  165. gdev_fax_print_page_stripped(gx_device_printer * pdev, FILE * prn_stream,
  166.             stream_CFE_state * ss, long rows)
  167. {
  168.     return gdev_stream_print_page_strips(pdev, prn_stream, &s_CFE_template,
  169.                      (stream_state *)ss, ss->Columns,
  170.                      rows);
  171. }
  172.  
  173. /* ---------------- Other TIFF output ---------------- */
  174.  
  175. #include "slzwx.h"
  176. #include "srlx.h"
  177.  
  178. /* Device descriptors for TIFF formats other than fax. */
  179. private dev_proc_print_page(tifflzw_print_page);
  180. private dev_proc_print_page(tiffpack_print_page);
  181.  
  182. const gx_device_tfax gs_tifflzw_device = {
  183.     prn_device_std_body(gx_device_tfax, prn_std_procs, "tifflzw",
  184.             DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  185.             X_DPI, Y_DPI,
  186.             0, 0, 0, 0,    /* margins */
  187.             1, tifflzw_print_page)
  188. };
  189.  
  190. const gx_device_tfax gs_tiffpack_device = {
  191.     prn_device_std_body(gx_device_tfax, prn_std_procs, "tiffpack",
  192.             DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  193.             X_DPI, Y_DPI,
  194.             0, 0, 0, 0,    /* margins */
  195.             1, tiffpack_print_page)
  196. };
  197.  
  198. /* Define the TIFF directory we use, beyond the standard entries. */
  199. /* NB: this array is sorted by tag number (assumed below) */
  200. typedef struct tiff_mono_directory_s {
  201.     TIFF_dir_entry BitsPerSample;
  202.     TIFF_dir_entry Compression;
  203.     TIFF_dir_entry Photometric;
  204.     TIFF_dir_entry FillOrder;
  205.     TIFF_dir_entry SamplesPerPixel;
  206.     TIFF_dir_entry T4T6Options;
  207.     /* Don't use CleanFaxData. */
  208.     /*  TIFF_dir_entry  CleanFaxData;   */
  209. } tiff_mono_directory;
  210. private const tiff_mono_directory dir_mono_template =
  211. {
  212.     {TIFFTAG_BitsPerSample, TIFF_SHORT, 1, 1},
  213.     {TIFFTAG_Compression, TIFF_SHORT, 1, Compression_CCITT_T4},
  214.     {TIFFTAG_Photometric, TIFF_SHORT, 1, Photometric_min_is_white},
  215.     {TIFFTAG_FillOrder, TIFF_SHORT, 1, FillOrder_LSB2MSB},
  216.     {TIFFTAG_SamplesPerPixel, TIFF_SHORT, 1, 1},
  217.     {TIFFTAG_T4Options, TIFF_LONG, 1, 0},
  218.     /* { TIFFTAG_CleanFaxData,      TIFF_SHORT, 1, CleanFaxData_clean }, */
  219. };
  220.  
  221. /* Forward references */
  222. private int tfax_begin_page(P4(gx_device_tfax *, FILE *,
  223.                    const tiff_mono_directory *, int));
  224.  
  225. /* Print a fax-encoded page. */
  226. private int
  227. tifff_print_page(gx_device_printer * dev, FILE * prn_stream,
  228.          stream_CFE_state * pstate, tiff_mono_directory * pdir)
  229. {
  230.     gx_device_tfax *const tfdev = (gx_device_tfax *)dev;
  231.     int code;
  232.  
  233.     tfax_begin_page(tfdev, prn_stream, pdir, pstate->Columns);
  234.     pstate->FirstBitLowOrder = true;    /* decoders prefer this */
  235.     code = gdev_fax_print_page_stripped(dev, prn_stream, pstate, tfdev->tiff.rows);
  236.     gdev_tiff_end_page(&tfdev->tiff, prn_stream);
  237.     return code;
  238. }
  239. private int
  240. tiffcrle_print_page(gx_device_printer * dev, FILE * prn_stream)
  241. {
  242.     stream_CFE_state state;
  243.     tiff_mono_directory dir;
  244.  
  245.     gdev_fax_init_fax_state(&state, (gx_device_fax *)dev);
  246.     state.EndOfLine = false;
  247.     state.EncodedByteAlign = true;
  248.     dir = dir_mono_template;
  249.     dir.Compression.value = Compression_CCITT_RLE;
  250.     dir.T4T6Options.tag = TIFFTAG_T4Options;
  251.     dir.T4T6Options.value = T4Options_fill_bits;
  252.     return tifff_print_page(dev, prn_stream, &state, &dir);
  253. }
  254. private int
  255. tiffg3_print_page(gx_device_printer * dev, FILE * prn_stream)
  256. {
  257.     stream_CFE_state state;
  258.     tiff_mono_directory dir;
  259.  
  260.     gdev_fax_init_fax_state(&state, (gx_device_fax *)dev);
  261.     state.EndOfLine = true;
  262.     state.EncodedByteAlign = true;
  263.     dir = dir_mono_template;
  264.     dir.Compression.value = Compression_CCITT_T4;
  265.     dir.T4T6Options.tag = TIFFTAG_T4Options;
  266.     dir.T4T6Options.value = T4Options_fill_bits;
  267.     return tifff_print_page(dev, prn_stream, &state, &dir);
  268. }
  269. private int
  270. tiffg32d_print_page(gx_device_printer * dev, FILE * prn_stream)
  271. {
  272.     stream_CFE_state state;
  273.     tiff_mono_directory dir;
  274.  
  275.     gdev_fax_init_state(&state, (gx_device_fax *)dev);
  276.     state.K = (dev->y_pixels_per_inch < 100 ? 2 : 4);
  277.     state.EndOfLine = true;
  278.     state.EncodedByteAlign = true;
  279.     dir = dir_mono_template;
  280.     dir.Compression.value = Compression_CCITT_T4;
  281.     dir.T4T6Options.tag = TIFFTAG_T4Options;
  282.     dir.T4T6Options.value = T4Options_2D_encoding | T4Options_fill_bits;
  283.     return tifff_print_page(dev, prn_stream, &state, &dir);
  284. }
  285. private int
  286. tiffg4_print_page(gx_device_printer * dev, FILE * prn_stream)
  287. {
  288.     stream_CFE_state state;
  289.     tiff_mono_directory dir;
  290.  
  291.     gdev_fax_init_state(&state, (gx_device_fax *)dev);
  292.     state.K = -1;
  293.     /*state.EncodedByteAlign = false; *//* no fill_bits option for T6 */
  294.     dir = dir_mono_template;
  295.     dir.Compression.value = Compression_CCITT_T6;
  296.     dir.T4T6Options.tag = TIFFTAG_T6Options;
  297.     return tifff_print_page(dev, prn_stream, &state, &dir);
  298. }
  299.  
  300. /* Print an LZW page. */
  301. private int
  302. tifflzw_print_page(gx_device_printer * dev, FILE * prn_stream)
  303. {
  304.     gx_device_tfax *const tfdev = (gx_device_tfax *)dev;
  305.     tiff_mono_directory dir;
  306.     stream_LZW_state state;
  307.     int code;
  308.  
  309.     dir = dir_mono_template;
  310.     dir.Compression.value = Compression_LZW;
  311.     dir.FillOrder.value = FillOrder_MSB2LSB;
  312.     tfax_begin_page(tfdev, prn_stream, &dir, dev->width);
  313.     state.InitialCodeLength = 8;
  314.     state.FirstBitLowOrder = false;
  315.     state.BlockData = false;
  316.     state.EarlyChange = 0;    /****** CHECK THIS ******/
  317.     code = gdev_stream_print_page(dev, prn_stream, &s_LZWE_template,
  318.                   (stream_state *) & state);
  319.     gdev_tiff_end_page(&tfdev->tiff, prn_stream);
  320.     return code;
  321. }
  322.  
  323. /* Print a PackBits page. */
  324. private int
  325. tiffpack_print_page(gx_device_printer * dev, FILE * prn_stream)
  326. {
  327.     gx_device_tfax *const tfdev = (gx_device_tfax *)dev;
  328.     tiff_mono_directory dir;
  329.     stream_RLE_state state;
  330.     int code;
  331.  
  332.     dir = dir_mono_template;
  333.     dir.Compression.value = Compression_PackBits;
  334.     dir.FillOrder.value = FillOrder_MSB2LSB;
  335.     tfax_begin_page(tfdev, prn_stream, &dir, dev->width);
  336.     state.EndOfData = false;
  337.     state.record_size = gdev_mem_bytes_per_scan_line((gx_device *) dev);
  338.     code = gdev_stream_print_page(dev, prn_stream, &s_RLE_template,
  339.                   (stream_state *) & state);
  340.     gdev_tiff_end_page(&tfdev->tiff, prn_stream);
  341.     return code;
  342. }
  343.  
  344. /* Begin a TIFF fax page. */
  345. private int
  346. tfax_begin_page(gx_device_tfax * tfdev, FILE * fp,
  347.         const tiff_mono_directory * pdir, int width)
  348. {
  349.     /* Patch the width to reflect fax page width adjustment. */
  350.     int save_width = tfdev->width;
  351.     int code;
  352.  
  353.     tfdev->width = width;
  354.     code = gdev_tiff_begin_page((gx_device_printer *) tfdev,
  355.                 &tfdev->tiff, fp,
  356.                 (const TIFF_dir_entry *)pdir,
  357.                 sizeof(*pdir) / sizeof(TIFF_dir_entry),
  358.                 NULL, 0, tfdev->MaxStripSize);
  359.     tfdev->width = save_width;
  360.     return code;
  361. }
  362.